var app = getApp()
var utils = require('../../utils/utils.js')
Page({
  data: {
    showTipWords:false,
    tipWords: '暂无订单'
  },
  onLoad: function(options) {
    var that = this
    // 获取用户信息
    wx.login({
      success: function (res) {
        //调用后台接口
        var param ={"code":res.code,"appId":app.globalData.appId};
        wx.request({
          url: utils.sysConst.getDeliveryClerkInfoPath,
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
          },
          data: param,
          dataType: "json",
          success: function(res) {
            if (res.data.code == 1){
              //设置全局用户信息-openid
              app.globalData.openid = res.data.data.openId
              if(res.data.data.register && res.data.data.status == 1){
                that.freshOrderList();
              }else{
                that.setData({
                  tipWords: "尚未注册配送员或账号不可用",
                  userOrder: null,
                  showTipWords:true,
                })
              }
            }else{
              wx.showToast({
                title:"获取用户信息失败："+res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
        // == getUserInfo end
      }
    });
    //wx.login === end ===
  },
  /**
   * 刷新当前页面
   * @param event
   */
  freshOrderList:function(event){
    var that = this
    var param = {"openId":"","status":"0"}
    wx.request({
      url: utils.sysConst.userOrderPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data: param,
      dataType: "json",
      success: function(res) {
        if (res.data.code == 1 && res.data.data != null){
          that.setData({
            tipWords: "",
            userOrder :res.data.data,
            showTipWords:false
          })
        }else{
          that.setData({
            tipWords: "暂无订单",
            userOrder: null,
            showTipWords:true,
          })
        }
      }
    })
  },

  //发货-start
  sendOrder:function(event){
    var that = this
    wx.showModal({
      title: '温馨提示',
      content: '确认发货订单',
      success (res) {
        if (res.confirm) {
          // start
          wx.showLoading({
            title: '处理中...',
          })
          var param = {"orderId":event.currentTarget.dataset.id,"openId":app.globalData.openid}
          wx.request({
            url: utils.sysConst.sendOrderPathPath,
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
            },
            data: param,
            dataType: "json",
            success: function(res) {
              wx.hideLoading();
              that.freshOrderList();
              if (res.data.code == 1){
                wx.showToast({
                  title: '操作成功',
                  icon: 'success',
                  duration: 2000
                })
              }else{
                wx.showToast({
                  title:"操作失败："+res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
          // end
        } else if (res.cancel) {
         // console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },




})

